// #ifdef H5
import h5Copy from "@/common/junyi-h5-copy/junyi-h5-copy.js"
// #endif

import Vue from "vue"
import { mapState } from "vuex"
import api from '@/api/index.js'
import { formatTime, plusXing, getAddress } from "@/utils/index.js"
export default {
	data() {
		return {
			request: false,
		}
	},
	computed: {
		...mapState({
			user: 'user_info',
		}),
		i18n_t() {
			return function(name) {
				return this.$t(`${name}`); // uniapp 不支持在取值表达式中直接调方法，因此，$t方法不可用，所以通过计算属性的方式：
			}
		},
		isLogin() {
			if (!this.$store.state.user_info) return false;
			return this.$store.state.user_info["uid"] > 0;
		},
		option: function() {
			return this.$store.state.option
		},
	},
	methods: {
		...{ formatTime, plusXing, getAddress },
		getUserInfo(type = null) {
			return new Promise((resolve, reject) => {
				this.api.get("/api/userinfo", {}).then(res => {
					let data = res["data"];
					const user = {
						uid: data ? data.uid : '0',
						balance: data ? data.balance : "0.00", //资金账户余额
						yitixian: data ? data.yitixian : "0.00",
						avatar: "../../static/images/avatar.png",
						level: data ? data.level : '0',
						status: data ? data["status"] : 0, //0普通会员 1VIP会员
						status_text: data ? data["status_text"] : '无效会员', //是否有效文字，后台返回
						address: data ? data.address : this.defaultAddress,  //这里从数据库取用户的地址
						parent: data ? data.address : '', //邀请人地址
						link: data ? data.link : ''
					}
					//console.log("更新全局个人信息", user);
					this.$store.commit("setUserInfo", user)
					resolve(user)
				}).catch(err => {
					this.apiErr(err)
					reject()
				})
			})
		},
		getImageSrc(src) {
			if (!src) return ''
			if (src.charAt(0) === '/') return this.$config.baseURL + src
			return src
		},

		goBack() {
			//  #ifdef H5
			history.back()
			// #endif

			// #ifndef H5
			uni.navigateBack({
				animationDuration: 200
			});
			// #endif
		},
		call(phone) {
			uni.makePhoneCall({
				phoneNumber: phone
			});
		},
		// 复制
		copy(content) {

			uni.setClipboardData({
				data: String(content),
				success: () => {
					uni.hideToast()
					uni.showToast({
						title: "复制成功"
					})
				}
			});

		},
		// 图片预览
		showImg(img, current = 0) {
			// 预览图片
			uni.previewImage({
				current: current,
				urls: img,
			});
		},
		openWin(url) {
			uni.navigateTo({
				url: url
			})
		},
		showModalView(content) {
			uni.showModal({
				title: this.i18n_t('提示'), //提示
				content: content,
				showCancel: false,
				confirmText: this.i18n_t('我知道了') //我知道了
			})
		},
		// 提示框
		toast(text, duration = 1200) {
			// #ifdef APP-PLUS
			plus.nativeUI.toast(text)
			// #endif

			// #ifndef APP-PLUS
			uni.showToast({
				title: text,
				duration: duration,
				icon: 'none'
			});
			// #endif

		},
		successBox(content) {
			uni.showModal({
				title: this.i18n_t.tips || '提示', //提示
				content: content,
				showCancel: false,
				confirmText: this.i18n_t.ok || '我知道了' //我知道了
			})
		},
		// 成功提示框
		success(text, duration = 1000) {
			if (this.$refs.uToast) {
				this.$refs.uToast.show({
					type: 'success',
					message: text,
					icon: false,
					duration: duration,
					position: 'bottom'
				})
			} else {
				uni.showToast({
					title: text,
					duration: duration,
					icon: 'none'
				});
			}

		},
		// 错误提示框
		fail(text, duration = 1500) {
			
			if (this.$refs.uToast) {
				this.$refs.uToast.show({
					type: 'error',
					icon: false,
					message: text,
					duration: duration,
					position: 'bottom'
				})
			} else {
				uni.showToast({
					title: text,
					duration: duration,
					icon: 'none'
				});
			}



		},
		// 显示加载框  //不会自动关闭
		loadingShow(text = '') {
			uni.showLoading({
				title: text ? text : "加载中", //加载中
				mask: true
			});
		},
		// 关闭加载框
		loadingHide() {
			uni.hideLoading()
		},

		// 接口报错提示
		apiErr(err) {
			this.loadingHide()
			if (err.msg) {
				this.fail(err.msg)
			} else {
				// this.fail("") //系统错误
			}
		},
		// 防抖处理
		isRequest() {
			if (this.request) return true;
			this.request = true;
			setTimeout(() => {
				this.request = false;
			}, 2000)
			return false;
		},

		uploadFile(FilePath) {

			return new Promise((resolve, reject) => {
				this.loadingShow(this.i18n_t('上传中...'))



				console.log('上传中...', FilePath);
				let upload_url = this.$config.baseURL + "/index/upload";
				uni.uploadFile({
					url: upload_url, //仅为示例，非真实的接口地址
					filePath: FilePath,
					name: 'file',
					header: {
						token: this.$store.state.token || ''
					},
					success: (uploadFileRes) => {
						this.loadingHide();
						if (uploadFileRes.statusCode !== 200) return reject()
						const data = JSON.parse(uploadFileRes.data)
						if (data.code !== 0) return reject()
						resolve(data["url"]);
					},
					fail: (err) => {
						this.loadingHide();
						reject()
					}
				});
			})
		},
		// 打开view
		openWebView(url, title) {
			// #ifdef H5
			this.openWin(`/pages/web-view/web-view?web=${encodeURIComponent(JSON.stringify(url))}&title=${title}`)
			// #endif

			// #ifdef APP-PLUS
			plus.runtime.openWeb(url);
			// #endif
		},
		checkLogin() {
			console.log(this.defaultAddress);
			if (this.isLogin && this.defaultAddress) return true
			this.showModalView(this.i18n_t('请先链接BNB钱包')) //请先链接BNB钱包
			return false
		},

	}
}